import { UPDATE_STATE, UPDATE_ALL_STATE, UPDATE_GOODS_COUNT, INIT_GOODS } from '../constant'

export default function cart(state = [], action) {
  switch (action.type) {
    // 更新指定商品的勾选状态
    case UPDATE_STATE:
      return state.map(item => {
        if (item.id === action.payload.id) {
          return { ...item, goods_state: action.payload.value }
        }
        return item
      })
    // 更新所有商品的勾选状态
    case UPDATE_ALL_STATE:
      return state.map(item => ({ ...item, goods_state: action.payload }))
    // 更新指定商品的勾选数量
    case UPDATE_GOODS_COUNT:
      return state.map(item => {
        if (item.id === action.payload.id) {
          return { ...item, goods_count: action.payload.count }
        }
        return item
      })
    // 初始化商品列表数据
    case INIT_GOODS:
      return action.payload
    // 默认操作
    default:
      return state
  }
}
